package com.dhcc.core.framework.base.controller;

import java.beans.PropertyEditorSupport;
import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.dhcc.core.framework.base.BaseLogger;
import com.dhcc.core.framework.base.warpper.BaseControllerWarpper;
import com.dhcc.core.framework.security.ShiroKit;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.framework.util.DateUtil;
import com.dhcc.core.framework.util.HttpContextUtil;
import com.dhcc.core.modules.system.entity.User;

/**
 * BaseController
 * 
 * @ClassName: BaseController
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午9:44:22
 */
public class BaseController extends BaseLogger{

    protected static String REDIRECT = "redirect:";
    protected static String FORWARD = "forward:";

    protected HttpServletRequest getHttpServletRequest() {
        return HttpContextUtil.getRequest();
    }

    protected HttpServletResponse getHttpServletResponse() {
        return HttpContextUtil.getResponse();
    }

    /**
     * getSession(boolean create)意思是返回当前reqeust中的HttpSession
     * ，如果当前reqeust中的HttpSession 为null，当create为true，就创建一个新的Session，否则返回null；
     * 简而言之： HttpServletRequest.getSession(ture)等同于
     * HttpServletRequest.getSession() HttpServletRequest.getSession(false)等同于
     * 如果当前Session没有就为null； 具体的使用场景: 当向Session中存取登录信息时，一般建议：HttpSession session
     * =request.getSession(); 当从Session中获取登录信息时，一般建议：HttpSession session
     * =request.getSession(false);
     */
    protected HttpSession getSession() {
        return HttpContextUtil.getRequest().getSession();
    }

    protected HttpSession getSession(Boolean flag) {
        return HttpContextUtil.getRequest().getSession(flag);
    }

    protected String getContextPath() {
        return HttpContextUtil.getRequest().getContextPath();
    }

    protected String getParameter(String name) {
        return HttpContextUtil.getRequest().getParameter(name);
    }

    protected void setAttribute(String name, Object value) {
        HttpContextUtil.getRequest().setAttribute(name, value);
    }

    /**
     * 当前用户
     * 
     * @Title: getLoginUser
     * @Description: TODO
     * @return
     * @return: ShiroUser
     */
    protected User getLoginUser() {
        return ShiroKit.getUser();
    }

    /**
     * 当前用户id
     * 
     * @Title: getLoginUserId
     * @Description: TODO
     * @return
     * @return: Long
     */
    protected Long getLoginUserId() {
        if (CommonUtil.isEmpty(ShiroKit.getUser())) {
            return null;
        }
        return ShiroKit.getUser().getId();
    }

    /**
     * 当前用户是否是admin
     * 
     * @Title: isAdmin
     * @Description: TODO
     * @return
     * @return: boolean
     */
    protected boolean isAdmin() {
        return ShiroKit.isAdmin();
    }

    /**
     * 包装一个list或Object，让Object增加额外属性
     */
    protected Object warpObject(BaseControllerWarpper warpper) {
        return warpper.warp();
    }

    /**
     * 删除cookie
     */
    protected void deleteCookieByName(String cookieName) {
        Cookie[] cookies = this.getHttpServletRequest().getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(cookieName)) {
                Cookie temp = new Cookie(cookie.getName(), "");
                temp.setMaxAge(0);
                this.getHttpServletResponse().addCookie(temp);
            }
        }
    }

    /**
     * 时间格式化
     * 
     * @Title: initBinder
     * @Description: TODO
     * @param binder
     * @return: void
     */
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(DateUtil.parse(text));
            }
        });
    }
}
